<?php
class xl_order{
    function read(){
        global $dbh;
		$sql='SELECT * FROM orders ORDER BY id DESC';
		$sth=$dbh->prepare($sql);
		$sth->execute();
		return $sth->fetchAll();
    }
    function read_id($id){
        global $dbh;
        $sql='SELECT * FROM orders WHERE id=:id LIMIT 0,1';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return  $sth->fetch(PDO::FETCH_ASSOC);
    }
    function delete_all($id){
        global $dbh;
        $sql='SELECT * FROM orders WHERE id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth->fetch(PDO::FETCH_ASSOC);
    }
    function delete($id){
        global $dbh;
        $sql='DELETE FROM orders WHERE id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth;
    }
    function delete_detail($id){
        global $dbh;
        $sql='DELETE FROM orders_detail WHERE order_id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth;
    }
    function update_checkout($id){
        global $dbh;
        $sql='UPDATE orders SET checkout=1-checkout WHERE id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth;
    }
    function list_order_detail($id){
        global $dbh;
        $sql='SELECT detail.name,detail.model,detail.quantity,detail.price,detail.total,orders.date_added,product.image,product.day_time FROM orders,orders_detail AS detail,product WHERE detail.order_id = orders.id AND detail.product_id=product.id AND orders.id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth->fetchAll();
    }
}